﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Windows.Documents;

namespace CharacterCreation.Contracts {
    public class RichTextBoxHelper : DependencyObject {
        public static FlowDocument GetDocumentXaml(DependencyObject obj) { return (FlowDocument)obj.GetValue(DocumentXamlProperty); }
        public static void SetDocumentXaml(DependencyObject obj, FlowDocument value) { obj.SetValue(DocumentXamlProperty, value); }
        public static readonly DependencyProperty DocumentXamlProperty = DependencyProperty.RegisterAttached("DocumentXaml", typeof(FlowDocument), typeof(RichTextBoxHelper),
            new FrameworkPropertyMetadata() {
                BindsTwoWayByDefault = false,
                PropertyChangedCallback = (obj, e) => {
                    var richTextBox = (RichTextBox)obj;
                    richTextBox.Document = (FlowDocument)e.NewValue;



                    //// Parse the XAML to a document (or use XamlReader.Parse())
                    //var xaml = GetDocumentXaml(richTextBox);
                    //var doc = new FlowDocument();
                    //var range = new TextRange(doc.ContentStart, doc.ContentEnd);

                    //range.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)), DataFormats.Xaml);

                    //// Set the document
                    //richTextBox.Document = doc;

                    //// When the document changes update the source
                    //range.Changed += (obj2, e2) => {
                    //    if (richTextBox.Document == doc) {
                    //        MemoryStream buffer = new MemoryStream();
                    //        range.Save(buffer, DataFormats.Xaml);
                    //        SetDocumentXaml(richTextBox, Encoding.UTF8.GetString(buffer.ToArray()));
                    //    }
                    //};
                }
            });
    }
}
